package com.example.day20_zhongji.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.day20_zhongji.R;
import com.example.day20_zhongji.Sql;
import com.example.day20_zhongji.adapter.CradAdapter;
import com.example.day20_zhongji.base.BaseFragment;
import com.example.day20_zhongji.bean.ArticleBean;
import com.example.day20_zhongji.presenter.ArticlePresenter;
import com.example.day20_zhongji.view.ArticleView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CradFragment extends BaseFragment<ArticlePresenter> implements ArticleView {
    @BindView(R.id.crad_re)
    RecyclerView mcrad_re;
    private ArrayList<ArticleBean.DataBean.DatasBean> datasBeans;
    private CradAdapter cradAdapter;
    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mcrad_re.setLayoutManager(new LinearLayoutManager(getContext()));
        mcrad_re.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        datasBeans = new ArrayList<>();
        cradAdapter = new CradAdapter(getContext(), datasBeans);
        mcrad_re.setAdapter(cradAdapter);
        cradAdapter.setOnContextClickListener(new CradAdapter.OnContextClickListener() {
            @Override
            public void oncontextclicklistener(View v, int position) {
                Sql sql = new Sql(getContext());
                sql.insertcard(datasBeans.get(position).getShareUser(),datasBeans.get(position).getTitle());
                sql.close();
                Toast.makeText(getContext(),"添加成功",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void initPresenter() {
        mPresenter=new ArticlePresenter();
        mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_crad;
    }

    @Override
    public void onSuccess(ArticleBean data) {
        datasBeans.addAll(data.getData().getDatas());
        cradAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {

    }

    @Override
    public void showToast(String str) {

    }
}
